import shutil
import os
import re
import sys
import code,traceback
import subprocess
import fnmatch
try:
    import PyInstaller.makespec as makespec
    import PyInstaller.build as build
except:
    raise Exception("PyInstaller needs to be on your PYTHONPATH.")

source_dir = os.path.dirname(__file__) if '__file__' in globals() else os.curdir()
build_dir = os.path.join(source_dir, 'build')

if not os.path.isdir(build_dir):
    os.mkdir(build_dir)

def make_specs(cmd_out='hydmaker'):
    reload(makespec)
    cmd_script = os.path.join(source_dir, 'hydmaker.py')

    makespec.main(scripts=[cmd_script], name=cmd_out, onefile=True, 
            console=True, debug=False, strip=None, noupx=False, 
            comserver=0, workdir=build_dir, pathex=[source_dir], version_file=None,
            icon_file=None, manifest=None, resources=[], crypt=None, hiddenimports=None,
            hookspath=None, **{'loglevel' : 'INFO'})

def Build(specs=True):
    cmd_out = 'hydmaker'

    if specs:
        make_specs(cmd_out=cmd_out)

    cmd_spec = os.path.join(build_dir, cmd_out + '.spec')
    buildpath = 'SPECPATH\\build\\pyi.TARGET_PLATFORM\\SPECNAME'
    kw = {'loglevel' : 'INFO', 'upx_dir' : None}
    build.main(specfile=cmd_spec, buildpath=buildpath, noconfirm=False, ascii=None, **kw)
    


